package com.hs.controller;

import com.github.pagehelper.PageInfo;
import com.hs.entity.Consultant;
import com.hs.service.ConsultantService;
import com.hs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("consultant")
public class ConsultantController {
    @Autowired
    private ConsultantService consultantService;
    @PostMapping
    public R addConsultant(@RequestBody Consultant consultant) {
        Integer i = consultantService.addConsultant(consultant);
        if (i > 0) {
            return R.ok();
        }else {
            return R.fail("失败");
        }
    }
    @DeleteMapping("{id}")
    public R deleteConsultant(@PathVariable("id") Integer id) {
        Integer i = consultantService.deleteConsultant(id);
        if (i > 0) {
            return R.ok();
        }else {
            return R.fail("失败");
        }
    }
    @PutMapping
    public R updateConsultant(@RequestBody Consultant consultant) {
        Integer i = consultantService.updateConsultant(consultant);
        if (i > 0) {
            return R.ok();
        }else {
            return R.fail("失败");
        }
    }
    @GetMapping("{id}")
    public R getConsultantById(@PathVariable("id") Integer id) {
        Consultant consultant = consultantService.getConsultantById(id);
        return R.ok(consultant);
    }
    @GetMapping("page")
    public R getConsultantByPage(@RequestParam(defaultValue = "1",required = false) Integer pageNum,
                                 @RequestParam(defaultValue = "5",required = false) Integer pageSize) {
        PageInfo<Consultant> consultantPage = consultantService.getConsultantPage(pageNum,pageSize);
        return R.ok(consultantPage);
    }
    @GetMapping
    public R getConsultantList(Consultant consultant) {
        PageInfo<Consultant> allConsultantlist = consultantService.getAllConsultantlist(consultant);
        return R.ok(allConsultantlist);
    }
    @GetMapping("pageByName")
    public R getConsultantByLevelName(String levelName,String tag,String address,String gender,
                                      @RequestParam(required = false,defaultValue = "1")int pageNum,
                                      @RequestParam(required = false,defaultValue = "5")int pageSize) {
        PageInfo<Consultant> consultantByLevelName = consultantService.getConsultantByLevelName(levelName,tag,address,gender,pageNum, pageSize);
        return R.ok(consultantByLevelName);
    }
    @GetMapping("getConsultantByLevelNameAndTag")
    public R getConsultantByLevelNameAndTag(String levelName,String tag){
        List<Consultant> consultantByLevelNameAndTag = consultantService.getConsultantByLevelNameAndTag(levelName, tag);
        return R.ok(consultantByLevelNameAndTag);
    }
}
